package com.waterwolf.wolfbot;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import org.jdom.Element;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;

import com.rshacking.rhf.files.XMLContainer;

public class ClientClassLoader extends ClassLoader {

    private JarFile rsJar;
    
    HashMap<String, Class> classes = new HashMap<String, Class>();

    File injectedClasses = new File("injClasses");
    
    public ClientClassLoader() {
        

        BasicLoader.setClassloader(this);

        if (!injectedClasses.exists()) {
            injectedClasses.mkdir();
        }
    }

    public Class<?> _defineClass(final String name, final byte[] b,
            final int off, final int len,
            final ProtectionDomain protectionDomain) {
        if (classes.containsKey(name))
            return classes.get(name);
        Class<?> c = null;
        try {
            c = super.findSystemClass(name);
        } catch (final ClassNotFoundException cnfe) {
            final byte[] buffer = BasicLoader.classes.get(name);
            c = defineClass(name, buffer, 0, buffer.length);
        }
        classes.put(name, c);
        return c;
    }

    @Override
    public Class<?> loadClass(final String name) {
        if (name.equals("bq")) {
            System.out.println("asked for barbeque");
        }
        if (classes.containsKey(name))
            return classes.get(name);
        Class<?> c = null;
        try {
            c = super.findSystemClass(name);
        } catch (final ClassNotFoundException cnfe) {
            final byte[] buffer = BasicLoader.classes.get(name);
            if (buffer == null) {
                try {
                    return super.loadClass(name);
                } catch (final ClassNotFoundException e) {
                    System.err.println(name + " not found");
                    return null;
                }
            }
            
            try {
                final BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(new File(injectedClasses, name+"_dec.class")));
                fos.write(buffer);
                fos.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
            
            c = defineClass(name, buffer, 0, buffer.length);
        }
        classes.put(name, c);

        return c;
    }
}
